#import "BirthdayUI.h"

@implementation BirthdayUI
@synthesize dic;
@synthesize datePickerView;
@synthesize selectLabel,year,yearArray,month,monthArray;
@synthesize dayArray;
@synthesize hourArray,day,hour;
-(void) addLabel {
    
}

- (void)viewDidLoad {
    @try{
        [super viewDidLoad];
        
        [self addLabel];
        
        self.datePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0, 320, 162)];
        self.datePickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.datePickerView.showsSelectionIndicator = YES;
        self.datePickerView.delegate = self;
        self.datePickerView.dataSource = self;
        self.datePickerView.showsSelectionIndicator = YES;
        
        [[UIPickerView appearance] setBackgroundColor:[UIColor whiteColor]];
        
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *now;
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
        NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        now=[NSDate date];
        comps = [calendar components:unitFlags fromDate:now];
        int y = [comps year];
        
        
        
        year=[NSString stringWithFormat:@"%d",y];
        
        
        int m = [comps month];
        month=[NSString stringWithFormat:@"%d",m];
        int d = [comps day];
        day=[NSString stringWithFormat:@"%d",d];
        int h = [comps hour];
        hour=[NSString stringWithFormat:@"%d",h];
        
        
        
        [self.datePickerView selectRow:y inComponent:0 animated:NO];
        
        
        //[self.datePickerView selectRow:m-1 inComponent:1 animated:NO];
        
        //[self.datePickerView selectRow:d-1 inComponent:2 animated:NO];
        
       
        
        
        
        
        
        [self.view addSubview:self.datePickerView];
        
        self.yearArray= [[NSMutableArray alloc]initWithCapacity:2];
        int  last=y;
        y=y-20;
        [self.yearArray addObject:@"选择年份"];
        for(int i=y;i<=last;i++){
            [self.yearArray addObject:[NSString stringWithFormat:@"%d年",i]];
        }
        
        
        
        
        self.monthArray= [[NSMutableArray alloc]initWithCapacity:12];
        for(int i=1;i<=12;i++){
            [self.monthArray addObject:[NSString stringWithFormat:@"%d月",i]];
        }
        
        self.dayArray= [[NSMutableArray alloc]initWithCapacity:31];
        for(int i=1;i<=31;i++){
            [self.dayArray addObject:[NSString stringWithFormat:@"%d日",i]];
        }
        
        
        NSLog(@"self.monthArray长度=%d",[self.monthArray count]);
        
        
        
        [UIView beginAnimations: @"Animation" context:nil];
        [UIView setAnimationDuration:0.1];
        datePickerView.frame = CGRectMake(0, 0, 320,162.0);
        [UIView commitAnimations];
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
    
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    NSString *str = @"";
    @try{
        if (component == 0) {
            
            //NSLog(@"self.yearArray==%@",self.yearArray);
            
            self.year=[self.yearArray objectAtIndex:row];
            
            return self.year;
            
            
            
        }
        
        
        if (component == 1) {
            self.month=[self.monthArray objectAtIndex:row];
            
            
            
            return self.month;
        }
        if (component == 2) {
            self.day=[self.dayArray objectAtIndex:row];
            
            
            
            return self.day;
            
        }
        if (component == 3) {
            self.hour=[self.hourArray objectAtIndex:row];
            
            return self.hour;
            
            
            
        }
        
        
        
        
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
    
    
    return str;
    
    
}




- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger integer;
    @try{
        if (component == 0){
            return 20+1;
        }
        if(component==1){
            return 12;
        }
        if(component==2){
            return 31;
        }
       
        
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
    
    return integer;
    
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    CGFloat componentWidth = 0.0;
    
    if (component == 0)
    {
        componentWidth =140;
    }else if (component == 3)
    {
        componentWidth =80;
    }
    
    else
    {
        componentWidth =80;
    }
    
    
    return componentWidth;
}

//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
//{
//    return 40.0;
//}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 30.0f;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    @try{
        NSLog(@"component==%d",component);
        if (component ==0) {
            self.year=[self.yearArray objectAtIndex:row] ;
            
            rowA=row;
            
            
            NSMutableString* tmp=[[NSMutableString alloc] init];
            [tmp appendFormat:@"%@",self.year];
            [tmp stringByAppendingString:@"-"];
            
            [tmp appendFormat:@"%@",self.month];
            [tmp stringByAppendingString:@"-"];
            
            
            
            [tmp appendFormat:@"%@",self.day];
            
            [tmp stringByAppendingString:@" "];
            [tmp appendFormat:@"%@",self.hour];
            
            
            
            dmyDic=[NSDictionary dictionaryWithObjectsAndKeys:tmp,@"time", nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"setFromTimeNS" object:dmyDic];
            
            [self.datePickerView selectRow:rowA inComponent:0 animated:NO];
            
        }
        
        
        if (component == 1) {
            
            self.month=[self.monthArray objectAtIndex:row];
            
            
            NSMutableString* tmp=[[NSMutableString alloc] init];
            
            [tmp appendFormat:@"%@",self.year];
            [tmp stringByAppendingString:@"-"];
            
            
            [tmp appendFormat:@"%@",self.month];
            [tmp stringByAppendingString:@"-"];
            
            
            
            [tmp appendFormat:@"%@",self.day];
            
            [tmp stringByAppendingString:@" "];
            [tmp appendFormat:@"%@",self.hour];
            
            
            
            dmyDic=[NSDictionary dictionaryWithObjectsAndKeys:tmp,@"time", nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"setFromTimeNS" object:dmyDic];
            
            rowB=row;
            
            
            [self.datePickerView selectRow:rowB inComponent:1 animated:NO];
            
            
            
            
        }
        if (component == 2) {
            
            self.day=[self.dayArray objectAtIndex:row];
            
            NSMutableString* tmp=[[NSMutableString alloc] init];
            
            [tmp appendFormat:@"%@",self.year];
            [tmp stringByAppendingString:@"-"];
            
            
            [tmp appendFormat:@"%@",self.month];
            [tmp stringByAppendingString:@"-"];
            
            
            
            [tmp appendFormat:@"%@",self.day];
            
            [tmp stringByAppendingString:@" "];
            [tmp appendFormat:@"%@",self.hour];
            
            
            rowC=row;
            dmyDic=[NSDictionary dictionaryWithObjectsAndKeys:tmp,@"time", nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"setFromTimeNS" object:dmyDic];
            
            
            [self.datePickerView selectRow:rowC inComponent:2 animated:NO];
        }
        
        
    }@catch (NSException* e) {
        NSLog(@"%@",e);
    }
    
}

//PickerView的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    
}


@end


